package com.nyist.diseasepreventionserver.controller;


import cn.hutool.json.JSON;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.nyist.diseasepreventionserver.VO.*;
import com.nyist.diseasepreventionserver.common.ResponseCode;
import com.nyist.diseasepreventionserver.common.ServerResponse;
import com.nyist.diseasepreventionserver.config.EpiReturnConfig;
import com.nyist.diseasepreventionserver.config.GsonVOConfig;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.nyist.diseasepreventionserver.config.EpidemicConfig.calcAuthorization;
import static com.nyist.diseasepreventionserver.config.EpidemicConfig.urlencode;

@RestController
@RequestMapping("/epidemic")
@Slf4j
public class EpidemicController {
    @GetMapping("/abroad")
    public ServerResponse epidemicData(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
        // url参数拼接
        String url = "http://service-3k8gkc1x-1255468759.sh.apigw.tencentcs.com/release/foreign_yq";
        EpiReturnConfig epiReturnConfig = new EpiReturnConfig();
        String returnConfig = epiReturnConfig.returnConfig(url,"","");
        Gson gson = new Gson();
        ShowapiVO showapiVO = gson.fromJson(returnConfig, ShowapiVO.class);
        EpiVO showapiResBody = showapiVO.getShowapiResBody();
        List<DetailVO> detailVO = showapiResBody.getDetailVO();
        if (detailVO == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.Epidemic_data_error.getCode(),
                    ResponseCode.Epidemic_data_error.getDesc());
        }else {
            return ServerResponse.createBySuccess(detailVO);
        }
    }
    @GetMapping("/trip")
    public ServerResponse tripData(String date,String search) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
        String url = "http://service-3k8gkc1x-1255468759.sh.apigw.tencentcs.com/release/travel";
        GsonVOConfig gsonVOConfig = new GsonVOConfig();
        EpiVO showapiResBody = gsonVOConfig.ReturnVOConfig(url,date,search);
        List<TripDateVO> tripDateVO = showapiResBody.getTripDateVO();
        if (tripDateVO.isEmpty()){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.trip_data_error.getCode(),
                    ResponseCode.trip_data_error.getDesc());
        }else {
            return ServerResponse.createBySuccess(tripDateVO);
        }
    }
    @GetMapping("/news")
    public ServerResponse news(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException{
        String url = "http://service-3k8gkc1x-1255468759.sh.apigw.tencentcs.com/release/fy_news";
        GsonVOConfig gsonVOConfig = new GsonVOConfig();
        EpiVO showapiResBody = gsonVOConfig.ReturnVOConfig(url,"","");
        List<NewsVO> newsVO = showapiResBody.getNewsVO();
        if (newsVO.isEmpty()){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.news_data_error.getCode(),
                    ResponseCode.news_data_error.getDesc());
        }else {
            return ServerResponse.createBySuccess(newsVO);
        }
    }
   @GetMapping("/today")
    public ServerResponse today(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException{
        String url = "http://service-3k8gkc1x-1255468759.sh.apigw.tencentcs.com/release/fy_details";
        GsonVOConfig gsonVOConfig = new GsonVOConfig();
        EpiVO showapiResBody = gsonVOConfig.ReturnVOConfig(url,"","");

       if (showapiResBody == null){
           return ServerResponse.createByErrorCodeMessage(ResponseCode.news_data_error.getCode(),
                   ResponseCode.news_data_error.getDesc());
       }else {
           return ServerResponse.createBySuccess(showapiResBody);
       }
   }

}
